package com.lhz.model.response;


import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhz.model.param.BaseQueryParam;

import java.util.List;

/**
 * @Author: LiHuaZhi
 * @Version: 1.0
 **/
public class NewPageBean<T> {
    private Long total;
    private List<T> list;
    private MyPage pageInfo;

    /**
     * @param select 调用Mapper接口的结果
     * @param param  请求参数
     * @param tClass 响应的Class对象
     * @param <T>
     * @return
     */
    public static <T> NewPageBean<T> generatePage(ISelect select, BaseQueryParam param, Class<T> tClass) {
        Integer current = param.getPageNum();
        Integer pageSize = param.getPageSize();
        List<T> list = PageHelper
                .startPage(current == null ? 1 : current, pageSize == null ? 10 : pageSize)
                .doSelectPage(select);
        return new NewPageBean<>(list);
    }

    public NewPageBean(List<T> list) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        this.total = pageInfo.getTotal();
        this.list = list;
        MyPage page = new MyPage();
        page.setTotalPage(pageInfo.getPages());
        page.setPageNum(pageInfo.getPageNum());
        page.setPageSize(pageInfo.getPageSize());
        page.setPreviousPage(pageInfo.getPrePage());
        page.setNextPage(pageInfo.getNextPage());
        this.pageInfo = page;
    }

    public long getNum() {
        return this.total;
    }

    public void setNum(Long total) {
        this.total = total;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public MyPage getPageInfo() {
        return pageInfo;
    }

    public void setPageInfo(MyPage pageInfo) {
        this.pageInfo = pageInfo;
    }

    class MyPage {
        private int pageNum;
        private int pageSize;
        private int totalPage;
        private int nextPage;
        private int previousPage;

        public int getPageNum() {
            return pageNum;
        }

        public void setPageNum(int pageNum) {
            this.pageNum = pageNum;
        }

        public int getPageSize() {
            return pageSize;
        }

        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }

        public int getTotalPage() {
            return totalPage;
        }

        public void setTotalPage(int totalPage) {
            this.totalPage = totalPage;
        }

        public int getNextPage() {
            return nextPage;
        }

        public void setNextPage(int nextPage) {
            this.nextPage = nextPage;
        }

        public int getPreviousPage() {
            return previousPage;
        }

        public void setPreviousPage(int previousPage) {
            this.previousPage = previousPage;
        }
    }
}
